// File provided for Microsoft Shared Source.
// Copyright (c) Microsoft Corporation. All rights reserved.

#include "StdAfx.h"
#include "StrRet.h"

HRESULT SetSTRRET(STRRET *pStrRet, PCWSTR psz)
{
    HRESULT hr;

    size_t cch = wcslen(psz) + 1;
    pStrRet->uType = STRRET_WSTR;
    pStrRet->pOleStr = reinterpret_cast<PWSTR>(CoTaskMemAlloc(cch * sizeof(*psz)));
    hr = pStrRet->pOleStr ? S_OK : E_OUTOFMEMORY;
    if (SUCCEEDED(hr))
    {
        hr = StringCchCopy(pStrRet->pOleStr, cch, psz);
    }

    return hr;
}

HRESULT SetSTRRET(STRRET *pStrRet, DWORD dwResId, HINSTANCE hinst)
{
    HRESULT hr;

    WCHAR psz[4096];
    hr = LoadStringW(hinst, dwResId, psz, ARRAYSIZE(psz)) ? S_OK : HRESULT_FROM_WIN32(GetLastError());
    if (SUCCEEDED(hr))
    {
        hr = SetSTRRET(pStrRet, psz);
    }

    return hr;
}